<?php

class DBMJoinTranslator implements DBMTranslator {
	public static function translateToMySQL($join, $command = null) {
		// get the join type
		$type			= $join->getType();
		$filterGroups	= $join->getFilterGroups();
		$entity			= DBMEntityTranslator::translateToMySQL($join->getEntity(), $command);
		$sql			= "";
		
		// create the basic structure of the join
		if($type == DBMJoin::INNER_JOIN){
			$sql = "INNER JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::LEFT_JOIN){
			$sql = "LEFT JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::OUTER_JOIN){
			$sql = "OUTER JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::RIGHT_JOIN){
			$sql = "RIGHT JOIN " . $entity . " ON ";
		} else {
			return "";
		}
		
		// translate the filter groups
		$translatedFilterGroups = array();
		foreach($filterGroups as $filterGroup){
			$translatedFilterGroups[] = DBMFilterGroupTranslator::translateToMySQL($filterGroup, $command);
		}
		
		// add the filter groups the join structure
		$sql .= implode(" AND ", $translatedFilterGroups);
		
		return $sql;
	}
	public static function translateToPostgreSQL($join, $command = null) {
		// get the join type
		$type				= $join->getType();
		$filterGroups		= $join->getFilterGroups();
		$entity				= DBMEntityTranslator::translateToPostgreSQL($join->getEntity(), $command);
		$sql				= "";
		
		// create the basic structure of the join
		if($type == DBMJoin::INNER_JOIN){
			$sql = "INNER JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::LEFT_JOIN){
			$sql = "LEFT JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::OUTER_JOIN){
			$sql = "OUTER JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::RIGHT_JOIN){
			$sql = "RIGHT JOIN " . $entity . " ON ";
		} else {
			return "";
		}
		
		// translate the filter groups
		$translatedFilterGroups = array();
		foreach($filterGroups as $filterGroup){
			$translatedFilterGroups[] = DBMFilterGroupTranslator::translateToPostgreSQL($filterGroup, $command);
		}
		
		// add the filter groups the join structure
		$sql .= implode(" AND ", $translatedFilterGroups);
		
		return $sql;
	}
	public static function translateToSQLServer($join, $command = null) {
		// get the join type
		$type			= $join->getType();
		$filterGroups	= $join->getFilterGroups();
		$entity			= DBMEntityTranslator::translateToSQLServer($join->getEntity(), $command);
		$sql			= "";
		
		// create the basic structure of the join
		if($type == DBMJoin::INNER_JOIN){
			$sql = "INNER JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::LEFT_JOIN){
			$sql = "LEFT JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::OUTER_JOIN){
			$sql = "OUTER JOIN " . $entity . " ON ";
		} elseif($type == DBMJoin::RIGHT_JOIN){
			$sql = "RIGHT JOIN " . $entity . " ON ";
		} else {
			return "";
		}
		
		// translate the filter groups
		$translatedFilterGroups = array();
		foreach($filterGroups as $filterGroup){
			$translatedFilterGroups[] = DBMFilterGroupTranslator::translateToSQLServer($filterGroup, $command);
		}
		
		// add the filter groups the join structure
		$sql .= implode(" AND ", $translatedFilterGroups);
		
		return $sql;
	}
}

?>